﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Multiplexor;
using SdmxMl.Structure;

namespace SdmxMl.Panels
{
    public partial class dlgGroupSelect : Form
    {
        public string SelectedFolder { get; private set; }
        public string SelectedGroup { get; private set; }
        public OrganisationSchemesType orgNsi { get; private set; }

        public dlgGroupSelect()
        {
            InitializeComponent();
            if (Mux.Instance.State == MultiplexorState.OkMultiplex)
            {
                foreach (CodeType c in Mux.Instance.ClGroup.CodeList)
                    comboBoxGroup.Items.Add(c);
            }
            else
            {
                // Fill combo agencies with NSI agencies
                orgNsi = SdmxMl.Msg.HeaderGenerator.NSI_OrgScheme;
                if (orgNsi != null)
                {
                    foreach (OrganisationType o in orgNsi.Agencies)
                        comboBoxGroup.Items.Add(o.Id);
                }

            }
            RefreshButtons();

        }

        private void buttonFolder_Click(object sender, EventArgs e)
        {

            DialogResult drSelectFolder;
            drSelectFolder = fbdSelectFolder.ShowDialog();

            if (drSelectFolder == DialogResult.OK)
            {
                // Check if folder is empty
                if (Directory.Exists(fbdSelectFolder.SelectedPath))
                {
                    var folder = new DirectoryInfo(fbdSelectFolder.SelectedPath);
                    if (folder.GetFileSystemInfos().Length == 0)
                    {
                        SelectedFolder = fbdSelectFolder.SelectedPath;
                        this.tbFolderSelecter.Text = SelectedFolder;
                    }
                    else
                    {
                        // Folder is not empty: show an information message
                        MessageBox.Show("Folder is not empty, please choose another one", "SDMX Import files", MessageBoxButtons.OK);
                    }
                }
            }
            RefreshButtons();
        }

        private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedGroup = null;
            if (comboBoxGroup.SelectedItem != null)
            {
                string sel = comboBoxGroup.SelectedItem.ToString();
                int index = sel.IndexOf(" ");
                if (index > 0)
                    sel = sel.Substring(0, index).Trim();
                SelectedGroup = sel;
            }
            RefreshButtons();
        }

        private void RefreshButtons()
        {
            buttonOk.Enabled = string.IsNullOrEmpty(SelectedGroup) == false &&
                string.IsNullOrEmpty(SelectedFolder) == false;
           
        }

    }
}
